package com.xiaoxie.boot.controller;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoxie.boot.bean.User;
import com.xiaoxie.boot.service.AdminService;
import com.xiaoxie.boot.utils.DataInfo;
import com.xiaoxie.boot.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @author xiaoxie
 * @create 2022-03-28 12:29
 */
@Api(tags = "管理员API")
@RestController
@Slf4j
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 查询所有管理员信息
     *
     * @param user
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryAllAdmin")
    @ApiOperation(value = "分页查询", notes = "查询所有管理员信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo queryAllAdmin(User user, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "15") Integer limit) {
        PageInfo<User> userPageInfo = adminService.selectAllAdminUser(user, page, limit);
        return DataInfo.ok("成功", userPageInfo.getTotal(), userPageInfo.getList());
    }

    /**
     * 管理员添加
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/addAdmin")
    @ApiOperation(value = "新增", notes = "管理员添加")
    @ApiOperationSupport(author = "小谢")
    public DataInfo addAdmin(@RequestBody User user, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        user.setCreateTime(DateUtil.getTimestamp());
        user.setLibraryCard(UUID.randomUUID().toString().substring(0, 6));
        user.setPassword("123456");
        user.setUserType(1);
        log.info(name + "添加了管理员：" + user.getUsername());
        int i = adminService.insertSelective(user);
        if (i == 1) {
            log.info("添加成功");
            return DataInfo.ok();
        } else {
            log.info("添加失败");
            return DataInfo.fail("用户添加失败!");
        }
    }

    /**
     * 修改管理员信息
     *
     * @param user
     * @param request
     * @return
     */
    @PutMapping("/editAdmin")
    @ApiOperation(value = "修改", notes = "修改管理员信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo editAdmin(@RequestBody User user, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        log.info(name + "修改了管理员：" + user.getUsername() + "的信息");
        int i = adminService.updateByPrimaryKeySelective(user);
        if (i == 1) {
            log.info("修改成功");
            return DataInfo.ok();
        } else {
            log.info("修改失败");
            return DataInfo.fail("用户修改失败!");
        }
    }

    /**
     * 删除管理员信息
     *
     * @param ids
     * @param request
     * @return
     */
    @DeleteMapping("/deleteAdmin")
    @ApiOperation(value = "删除", notes = "删除管理员信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo deleteAdmin(String ids, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        List<String> list = Arrays.asList(ids.split(","));
        log.info(name + "试图删管理员信息");
        int i = adminService.deleteUserByIds(list);
        if (i >= 1) {
            log.info("删除了：" + i + "条信息");
            return DataInfo.ok();
        } else {
            log.info("删除失败");
            return DataInfo.fail("删除失败");
        }
    }

    /**
     * 重置管理员密码
     *
     * @param id
     * @param request
     * @return
     */
    @PutMapping("/updatePasswordById")
    @ApiOperation(value = "重置密码", notes = "重置管理员密码")
    @ApiOperationSupport(author = "小谢")
    public DataInfo updatePasswordById(Integer id, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        log.info(name + "试图重置管理员密码");
        int i = adminService.updatePwdById(id);
        if (i == 1) {
            log.info("重置置管理员密码成功");
            return DataInfo.ok();
        } else {
            log.info("重置置管理员密码失败");
            return DataInfo.fail("重置失败");
        }
    }
}
